/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.c1202m.util;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author ThinhNV2
 */
public class DateUtil {

    private static final int FIRST_HOUR = 0;
    private static final int FIRST_MINUTE = 0;
    private static final int FIRST_SECOND = 0;
    private static final int FIRST_MILLISECOND = 0;

    public static Date getDayFirstTime(Date date) {

        if (null == date) {
            return null;
        } else {
            //set original date
            Calendar _orgCalendar = Calendar.getInstance();
            _orgCalendar.setTime(date);

            //set changed date
            Calendar _newCalendar = Calendar.getInstance();
            _newCalendar.set(_orgCalendar.get(Calendar.YEAR), _orgCalendar.get(Calendar.MONTH), _orgCalendar.get(Calendar.DAY_OF_MONTH), FIRST_HOUR, FIRST_MINUTE, FIRST_SECOND);
            _newCalendar.set(Calendar.MILLISECOND, FIRST_MILLISECOND);

            return _newCalendar.getTime();
        }
    }

    /**
     * return the last time of specified day.<br> ex:
     * <pre>
     *  // 2004/1/2 12:11:10
     * 	getDayFirstTime(new Date(2004, 0, 2, 12, 11, 10)
     *  	== Date(2004, 0, 2, 23, 59 ,59.999)	//2004/1/2 23:59:59.999
     * </pre>
     *
     * @param date Date
     * @return Date the last time of the specified day.
     */
    public static Date getDayLastTime(Date date) {

        if (null == date) {
            return null;
        } else {
            //set original date
            Calendar _orgCalendar = Calendar.getInstance();
            _orgCalendar.setTime(date);

            //set changed date
            Calendar _newCalendar = Calendar.getInstance();
            _newCalendar.set(_orgCalendar.get(Calendar.YEAR), _orgCalendar.get(Calendar.MONTH), _orgCalendar.get(Calendar.DAY_OF_MONTH) + 1, FIRST_HOUR, FIRST_MINUTE, FIRST_SECOND);
            _newCalendar.set(Calendar.MILLISECOND, FIRST_MILLISECOND);
            _newCalendar.setTimeInMillis(_newCalendar.getTimeInMillis() - 1);

            return _newCalendar.getTime();
        }
    }
}
